package com.boke.order.spi;

import com.boke.order.utils.RedisOpsUtil;
import com.boke.order.utils.SpringBeanFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * ,;,,;
 * ,;;'(    社
 * __      ,;;' ' \   会
 * /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 * ,;' \    /-.,,(   ) \    码
 * ) /       ) / )|    农
 * ||        ||  \)
 * (_\       (_\
 *
 * @author ：zjq
 * @date ：Created in 2021/07/11 13:55
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description: 全局唯一主键生成器【分库分表专用】
 **/
@Slf4j
public class OrderByRedisKeyGenerator implements ShardingKeyGenerator {

    // 写法一

//    @Getter
//    private final String type = "CUSTOM";

//    @Getter
//    @Setter
//    private Properties properties = new Properties();


    // 写法 二

    @Override
    public String getType() {
        return "CUSTOM";
    }

    // 不需要，sharding 底层帮你做了  否则你想添加其他的东西
    // 对应  spring.shardingsphere.sharding.tables.t_order.key-generator.props.redis.prefix
    /**
     * 不需要，sharding 底层帮你做了  否则你想添加其他的东西
     * 对应  spring.shardingsphere.sharding.tables.t_order.key-generator.props.redis.prefix 配置
     * 至于为何：
     * 源码追溯： org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration
     */
    private Properties properties = new Properties();

    @Override
    public Properties getProperties() {
        return properties;
    }

    @Override
    public void setProperties(Properties properties) {
        this.properties = properties;
    }


    /**
     * 生成18位订单编号:8位日期+3位平台ID+7位以上自增id
     * 需在分表策略当中配置如下属性
     * spring.shardingsphere.sharding.tables.t_order.key-generator.column=id
     * spring.shardingsphere.sharding.tables.t_order.key-generator.type=CUSTOM
     * spring.shardingsphere.sharding.tables.t_order.key-generator.props.worker.id=123
     * spring.shardingsphere.sharding.tables.t_order.key-generator.props.redis.prefix=order:id:prefix:
     */
    @Override
    public Comparable<?> generateKey() {
        StringBuilder sb = new StringBuilder();
        RedisOpsUtil redisOpsUtil = SpringBeanFactory.getBean("redisOpsUtil");

        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String key = properties.getProperty("redis.prefix") + date;
        //增长值
        Long increment = null;
        if (redisOpsUtil.hasKey(key)) {
            increment = redisOpsUtil.incr(key, 1);
        } else {
            increment = redisOpsUtil.incr(key, 1);
            redisOpsUtil.expire(key, 24, TimeUnit.HOURS);
        }
        sb.append(date);
        sb.append(String.format("%03d", Integer.parseInt(properties.getProperty("worker.id"))));
        String incrementStr = increment.toString();
        if (incrementStr.length() <= 7) {
            sb.append(String.format("%07d", increment));
        } else {
            sb.append(incrementStr);
        }
        return Long.parseLong(sb.toString());
    }


}
